#define RECORDER
//#define TRAINER

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BodySoundSimulator.CaseData;
using System.IO;
using System.Collections.ObjectModel;
using BodySoundSimulator.Recording;
using BodySoundSimulator.CommonElements;
using BodySoundSimulator.Training;
using BodySoundSimulator;
using BodySoundSimulator.AbsoluteMapData;

namespace BodySoundSimulator
{
    public partial class mainframe : Form, IApplicationServices
    {
        #region Fields
        Dictionary<string, CaseInformation> existingCases;
        AbsoluteBodyFeatures absoluteFeatures;
        #endregion

        #region Constructors
        public mainframe()
        {
            InitializeComponent();
            this.existingCases = GetExistingCaseInformation();
            this.absoluteFeatures = AbsoluteBodyFeaturesDataManipulator.LoadAbsoluteBodyFeatureData();
#if RECORDER
            this.pnlMain.Controls.Add(new Recorder(this));
#elif TRAINER
            this.pnlMain.Controls.Add(new Trainer(this));
#else 
            //start startup form
            StarterForm startUp = new StarterForm();
            if (startUp.ShowDialog() == DialogResult.OK)
            {
                //set mode
                switch (startUp.Mode)
                {
                    case ApplicationMode.Practicing:
                        //load practicing control
                        this.pnlMain.Controls.Add(new Trainer(this));
                        break;
                    case ApplicationMode.Recording:
                        //load recording control
                        this.pnlMain.Controls.Add(new Recorder(this));
                        break;
                    case ApplicationMode.Testing:
                        //load testing control
                        break;
                }
           }
#endif
        }
        #endregion

        #region Private methods
        Dictionary<string, CaseInformation> GetExistingCaseInformation()
        {
            Dictionary<string, CaseInformation> output = new Dictionary<string, CaseInformation>();
            string folderName = Path.Combine(Application.StartupPath, SimulatorFileManager.CasesFolderName);
            if (Directory.Exists(folderName))
            {
                //load case information
                string[] folders = Directory.GetDirectories(folderName);

                foreach (string name in folders)
                {
                    //extract case information from folder
                    string caseInformationXmlName = Path.Combine(name, string.Format("{0}.xml", Path.GetFileName(name)));
                    CaseInformation caseInfo = CaseInformation.XmlDeserialize(caseInformationXmlName);
                    if (File.Exists(caseInformationXmlName))
                    {
                        output.Add(caseInfo.CaseID, caseInfo);
                    }
                }
            }
            return output;
        }
        #endregion

        #region IApplicationServices Members

        public Dictionary<string, CaseInformation> ExistingCases
        {
            get 
            {
                return this.existingCases;
            }
        }

        public AbsoluteBodyFeatures AbsoluteFeatures
        {
            get 
            {
                return this.absoluteFeatures;
            }
        }

        public void ExistingCaseListChanged()
        {
            this.existingCases = GetExistingCaseInformation();
        }

        #endregion
    }
}